package com.tensquare.qa.dao;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.tensquare.qa.pojo.Problem;
import org.springframework.data.jpa.repository.Query;

/**
 * 数据访问接口.
 *
 * @author Administrator
 */
public interface ProblemDao
    extends JpaRepository<Problem, String>, JpaSpecificationExecutor<Problem> {

  /**
   * 最新回答.
   *
   * @param labelId 标签id
   * @param pageable 分页
   * @return
   */
  @Query(
      value =
          "SELECT * FROM tb_problem tp INNER JOIN tb_pl pl ON tp.id = pl.problemid WHERE pl.labelid = ? order by tp.replytime desc",
      nativeQuery = true)
  Page<Problem> newList(String labelId, Pageable pageable);

  /**
   * 最热回答.
   *
   * @param labelId 标签id
   * @param pageable 分页
   * @return
   */
  @Query(
      value =
          "SELECT * FROM tb_problem tp INNER JOIN tb_pl pl ON tp.id = pl.problemid WHERE pl.labelid = ? order by tp.replytime desc",
      nativeQuery = true)
  Page<Problem> hotList(String labelId, Pageable pageable);

  /**
   * 等待回答.
   *
   * @param labelId 标签id
   * @param pageable 分页
   * @return
   */
  @Query(
      value =
          "SELECT * FROM tb_problem tp INNER JOIN tb_pl pl ON tp.id = pl.problemid WHERE pl.labelid = ?  and  tp.reply = 0 order by tp.createtime desc",
      nativeQuery = true)
  Page<Problem> waitList(String labelId, Pageable pageable);
}
